import React from "react";
import BaseComponent from "../../../redare/components/BaseComponent";
import {Form, Input, Modal} from "antd";
import UserPresenter from "../../../.gen/presenter/UserPresenter";
import WebWorkbenchPresenter from "../../../.gen/presenter/WebWorkbenchPresenter";

export default class ModifyPasswordModal extends BaseComponent {

    formRef = React.createRef();

    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            loading: false
        };
        this.userPresenter = new UserPresenter(this);
        this.workbenchPresenter = new WebWorkbenchPresenter(this);
    }

    //确定修改密码
    updateLoginUserPwd() {
        let {user} = this._form;
        this.formRef.current.validateFields()
            .then(values => {
                let {pwd, pwd1} = values;
                if (pwd !== pwd1) {
                    this.toastWarning("两次输入密码不一致");
                    return
                }
                this.setButtonLoading(true);
                values.accountId = user.id;
                this.setState({confirmLoading: true});
                this.userPresenter.updateUserPwd(
                    values,
                    {},
                    () => {
                        this.setButtonLoading(false);
                        this.setModalVisible(false);
                        this.toastSuccess('密码修改成功');
                        this.workbenchPresenter.loginOut()
                    },
                    () => {
                        this.setButtonLoading(false)
                    }
                )
            })
    }

    _onLoginOutSuccess() {
        window.location.href = window._ctx + "/login?backUrl=" + window.location.href;
    }

    render() {
        return (
            <>
                <Modal
                    title="密码修改"
                    centered
                    okText={"确认修改"}
                    cancelText={'取消'}
                    confirmLoading={this.state.loading}
                    visible={this.state.visible}
                    onOk={() => this.updateLoginUserPwd()}
                    onCancel={() => this.setModalVisible(false)}
                    destroyOnClose={true}
                    maskClosable={false}
                >
                    <Form layout="vertical" ref={this.formRef}>
                        <Form.Item label="新密码" name="pwd">
                            <Input.Password placeholder="请输入" autoComplete="off"/>
                        </Form.Item>
                        <Form.Item label="密码确认" name="pwd1">
                            <Input.Password placeholder="请输入" autoComplete="off"/>
                        </Form.Item>
                    </Form>
                </Modal>
            </>
        );
    }

    _show(user) {
        this._form.user = user;
        this.setModalVisible(true)
    }

    setModalVisible(visible) {
        this.setState({visible})
    }

    setButtonLoading(loading) {
        this.setState({loading})
    }

}